package com.kaoshi.test28;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/25 10:27
 * @description
 */
public class Dao {

    ArrayList<Student> students = new ArrayList<>();
    Scanner input = new Scanner(System.in);

    {//预添加
        students.add(new Student(1,"王大",18,36));
        students.add(new Student(2,"王二",19,45));
        students.add(new Student(3,"王三",17,56));
    }

    //1.查询所有学生
    public void findAllstu(){
        for (Student student : students) {
            System.out.println("id:"+student.getSid()+
                    "，姓名："+student.getName()+
                    "，年龄："+student.getAge()+
                    "，成绩："+student.getScore());
        }
        System.out.println("=====================");
    }
    //2.添加学生
    public void addStudent(){
        System.out.print("请输入添加学生的编号：");
        int sid = input.nextInt();
        for (Student student : students) {
            if (sid==student.getSid()){
                System.err.println("此id已被占用");
                return;
            }
        }
        System.out.print("请输入添加学生的姓名：");
        String name = input.next();
        System.out.print("请输入添加学生的年龄：");
        int age = input.nextInt();
        System.out.print("请输入添加学生的成绩：");
        double score = input.nextDouble();
        Student student = new Student(sid,name,age,score);
        students.add(student);
        System.out.println("=====添加完成=====");
    }
    //3.修改学生信息
    public void editStuScore(){
        System.out.print("输入需要修改成绩的学生id：");
        int sid = input.nextInt();
        for (Student student : students) {
            if (student.getSid()==sid){
                System.out.print("输入修改后的成绩：");
                double score = input.nextDouble();
                student.setScore(score);
            }
        }
    }
    //4.删除学生信息
    public void reStu(){
        System.out.print("输入需要删除的学生id：");
        int sid = input.nextInt();
        for (int i = 0; i < students.size(); i++) {
            if (sid== students.get(i).getSid()){
                students.remove(i);
                i--;
            }
        }
    }
    //5.年龄从小到大排序
    public void sortAge(){
        int[] stuAges = new int[students.size()];
        for (int i = 0; i < students.size(); i++) {
            stuAges[i] = students.get(i).getAge();
        }
        for (int i = 0; i < stuAges.length; i++) {
            for (int j = 0; j < stuAges.length; j++) {
                if (stuAges[i]<stuAges[j]){
                    int temp = stuAges[i];
                    stuAges[i] = stuAges[j];
                    stuAges[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(stuAges));
        ArrayList<Student> studentsAge = new ArrayList<>();
        for (int i = 0; i < stuAges.length; i++) {
            for (Student student : students) {
                if (stuAges[i]==student.getAge()){
                    studentsAge.add(student);
                }
            }
        }

        for (Student student : studentsAge) {

            System.out.println("id:"+ student.getSid()+
                    "，姓名："+ student.getName()+
                    "，性别："+ student.getAge()+
                    "，成绩："+ student.getScore());
        }
    }
    //6.成绩从高到低
    public void sortScore(){
        double[] stusc = new double[students.size()];
        for (int i = 0; i < students.size(); i++) {
            stusc[i] = students.get(i).getScore();
        }
        for (int i = 0; i < stusc.length; i++) {
            for (int j = 0; j < stusc.length; j++) {
                if (stusc[i]>stusc[j]){
                    double temp = stusc[i];
                    stusc[i] = stusc[j];
                    stusc[j] = temp;
                }
            }
        }
        ArrayList<Student> studentsScore = new ArrayList<>();
        for (int i = 0; i < stusc.length; i++) {
            for (Student student : students) {
                if (stusc[i]==student.getScore()){
                    studentsScore.add(student);
                }
            }
        }
        for (Student student : studentsScore) {
            System.out.println("id:"+ student.getSid()+
                    "，姓名："+ student.getName()+
                    "，性别："+ student.getAge()+
                    "，成绩："+ student.getScore());
        }
    }
}
